package com.doris.media.picker.utils.permission

import android.app.Activity
import androidx.fragment.app.Fragment
import com.hjq.permissions.XXPermissions

object MyPermissionsUtils {

    @JvmStatic
    fun requestPermissionsTurn(
        activity: Activity, permissionObjective: String,
        listener: HavePermissionListener?, vararg permissions: String?,
    ) {
        XXPermissions.with(activity).permission(permissions)
            .interceptor(
                PermissionInterceptor(
                    PermissionNameConvert.getPermissionString(permissions.toMutableList()),
                    permissionObjective
                )
            ).request { _, allGranted ->
                if (allGranted) {
                    listener?.havePermission()
                } else {
                    listener?.noPermission()
                }
            }
    }

    @JvmStatic
    fun requestPermissionsTurn(
        fragment: Fragment, permissionObjective: String,
        listener: HavePermissionListener?, vararg permissions: String?,
    ) {
        XXPermissions.with(fragment).permission(permissions)
            .interceptor(
                PermissionInterceptor(
                    PermissionNameConvert.getPermissionString(permissions.toMutableList()),
                    permissionObjective
                )
            ).request { _, allGranted ->
                if (allGranted) {
                    listener?.havePermission()
                } else {
                    listener?.noPermission()
                }
            }
    }

    interface HavePermissionListener {
        fun havePermission()

        fun noPermission() {

        }
    }
}